﻿package emusic.api.clients 
{
	import emusic.api.EMusicClient;
	import emusic.ws.PaginationInfo;
	import flash.net.URLVariables;
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class AbstractSearchRequest
	{
		protected var _term:String = null;
		protected var _albumId:Number = NaN;
		protected var _artistId:Number = NaN;
		protected var _genreId:Number = NaN;
		protected var _labelId:Number = NaN;
		protected var _styleId:Number = NaN;
		protected var _requestUri:String = "";
		
		protected var _pagination:PaginationInfo;		
		
		protected var _format:String = "XML";
		protected var _callback:String = null;
		
		public function AbstractSearchRequest() 
		{
			_pagination = new PaginationInfo();
			_pagination.page = 1;
			_pagination.perPage = 20;			
		}
		
		public function get term():String { return _term; } 
		public function set term(value:String):void
		{ 
			resetAll();
			_term = value;
		}
		
		public function get albumId():Number { return _albumId; }
		public function set albumId(value:Number):void
		{ 
			resetAll();
			_term = null;
			_albumId = value;
		}
		
		public function get artistId():Number { return _artistId; }		
		public function set artistId(value:Number):void
		{ 
			resetAll();
			_artistId = value;
		}
		
		public function get genreId():Number { return _genreId; }		
		public function set genreId(value:Number):void
		{
			resetAll();
			_genreId = value; 
		}
		
		public function get labelId():Number { return _labelId; }
		public function set labelId(value:Number):void
		{ 
			resetAll();
			_labelId = value;
		}
		
		public function get styleId():Number { return _styleId; }
		public function set styleId(value:Number):void
		{
			resetAll();
			_styleId = value;
		}		
		
		public function get format():String { return _format; }
		public function set format(value:String):void { if (value != "JSONP") _callback = null;  _format = value; }

		public function get callback():String { return _callback; }		
		public function set callback(value:String):void { _format = "JSONP"; _callback = value; }
		
		public function get pagination():PaginationInfo { return _pagination; }		
		public function set pagination(value:PaginationInfo):void { _pagination = value; }
		
		public function get requestUri():String { return _requestUri; }
		
		protected function resetAll():void
		{
			_albumId = NaN;
			_artistId = NaN;
			_genreId = NaN;
			_labelId = NaN;
			_styleId = NaN;
			_term = null;			
		}		
		
		public function getVars():URLVariables
		{
			var vars:URLVariables = new URLVariables();
			vars.apiKey = EMusicClient.apiKey;
			vars.page = _pagination.page;
			vars.perPage = _pagination.perPage;
			vars.format = _format;
			
			if (_format == "JSONP")
				vars.callback = _callback;
				
			
			
			
			if (_term != null)			
				vars.term = _term;
			else if (!isNaN(_albumId))
				vars.albumId = _albumId;
			else if (!isNaN(_artistId))
				vars.artistId = _artistId;
			else if (!isNaN(_genreId))
				vars.genreId = _genreId;
			else if (!isNaN(_labelId))
				vars.labelId = _labelId;
			else if (!isNaN(_styleId))
				vars.styleId = _styleId;
			else
				throw new Error("You need to specify either a 'term', 'albumId', 'artistId', 'genreId', 'labelId' or 'styleId'; the request won't go through without any of them!");
				
			return vars;
		}
		
	}

}